package github.pop.ares.network;

import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.network.NetworkEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.Serializable;
import java.util.function.Supplier;

public class SendPack implements Serializable {
    private static final long serialVersionUID = 1L;

    private final String message;
    private static final Logger LOGGER = LogManager.getLogger();

    public SendPack(PacketBuffer buffer) {
        // decoding
        message = buffer.readUtf(Short.MAX_VALUE);
    }

    public SendPack(String message) {
        this.message = message;
    }

    public void toBytes(PacketBuffer buf) {
        // encoding
        buf.writeUtf(this.message);
    }

    public void handler(Supplier<NetworkEvent.Context> ctx) {
        // after receive data, how to use receive data
        ctx.get().enqueueWork(() -> {
            // simple print
            LOGGER.info(this.message);
            // use data logic must write here

        });
        // means execute finnish
        ctx.get().setPacketHandled(true);
    }
}
